﻿using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SassModule.Contracts;
using SassModule.Entity;
using SassModule.ViewModel;
using System;
using System.Collections.Generic;

namespace SassModule.Controllers
{
    /// <summary>
    /// UserController
    /// </summary>
    [ApiController]
    [Route("Sass/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IWebHostEnvironment _env;
        private readonly IUserService _service;
        private readonly IMapper _mapper;
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="env"></param>
        /// <param name="service"></param>
        public UserController(IMapper mapper,ILogger<UserController> logger, IWebHostEnvironment env, IUserService service)
        {
            _logger = logger;
            _env = env;
            _service = service;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取当前账套下所有的用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null,_service.Index(db)));
        }
        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        public IActionResult Index([FromRoute]Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null, _service.Index(id,db)));
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(User user)
        {
            string db = Common.GetHeader4DB(this.Request);
            var res= _service.Add(user,db);
            if (res!=null)
            {
                return Ok(new ResponseData(res, null));
            }
            return Ok(new ResponseData(null,null));
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Del(id, db);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("Reset")]
        public IActionResult Reset(UserPasswordViewModel user)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Reset(user, db);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 获取用户拥有的角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/roles")]
        public IActionResult GetRoles(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            var roles= _service.GetRoles(id, db);
            return Ok(new ResponseData(null, roles));
        }


        /// <summary>
        /// 设置用户的角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        [HttpPost("{id}/roles")]
        public IActionResult SetRoles(Guid id, [FromBody]List<Guid> roles)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.SetRoles(id, roles, db);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 个人设置（根据Id更新姓名和密码）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("setting")]
        public IActionResult PersonalSettings([FromBody] UserSetting user)
        {
            string dbStr = Common.GetHeader4DB(this.Request);
            var result= _service.Setting(user, dbStr);
            return Ok(new ResponseData(null, result));
        }

        [HttpPut("update")]
        public IActionResult UpdateRole(User info)
        {
            _service.UpdateUser(info);
            return Ok(new ResponseData(null, null));
        }
    }
}
